# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This source file is part of the Cangjie project, licensed under Apache-2.0
# with Runtime Library Exception.
#
# See https://cangjie-lang.cn/pages/LICENSE for license information.


from os import path
import random

integer_types = ['Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64']
other_types = ['Float16', 'Float32', 'Float64', 'String', 'Rune', 'Bool', 'Unit', '(Int8, Int8)', 'Array<Int8>', 'C']
types = integer_types + other_types
default_value_map = {'Bool' : 'true', 'Unit' : '()', 'Array<Int8>' : '[]', 'Rune' : "'1'", 'String' : '"1"', '(Int8, Int8)' : '(1, 1)', 'C' : 'C()'}
def default_value(ty : str) -> str:
  if ty in integer_types: return '1'
  if 'Float' in ty: return '1.0'
  return default_value_map[ty]

dir = path.dirname(path.realpath(__file__))
path = dir + '/test_' + path.basename(dir) + '_{}.cj'
binary_template = '''
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * This source file is part of the Cangjie project, licensed under Apache-2.0
 * with Runtime Library Exception.
 *
 * See https://cangjie-lang.cn/pages/LICENSE for license information.
 */

/*
  @Assertion:   4.20(2) The operands of the logical operators can only be of Bool types.
  @Description: Checks that using logical operator {op} with types {t1} and {t2} is not permitted.
  @Mode: compileonly
  @Negative: yes
  @Structure: single
  @CompileWarning: no
  @Comment: Auto-generated by gen.py
*/
{}
main() {{
    var v1 : {t1} = {}
    var v2 : {t2} = {}
    let v3 = v1 {op} v2
}}
'''
class_decl = '\nclass C {}\n'
inversion_template = '''
/*
 * Copyright (c). 2023-2023. All rights reserved.
 */

/*
  @Assertion:   4.20(2) The operands of the logical operators can only be of Bool types.
  @Description: Checks that using logical operator ! with type {t} is not permitted.
  @Mode: compileonly
  @Negative: yes
  @Structure: single
  @CompileWarning: no
  @Comment: Auto-generated by gen.py
*/
{}
main() {{
    var v : {t} = {}
    !v || true
}}
'''
limit = 100
random.seed(123)
counter = 2

def write_counted(contents : str):
  global counter
  with open(path.format(str(counter).zfill(3)), 'w') as file:
    file.write(contents)
    counter += 1

for op in ['&&', '||']:
  tests = [binary_template.format(class_decl if 'C' in (t1, t2) else '', default_value(t1), default_value(t2),
                    op=op, t1=t1, t2=t2)
  for t1 in types for t2 in types if not (t1 == t2 and t1 == 'Bool')]
  random.shuffle(tests)
  for test in tests[:limit]:
    write_counted(test)

tests = [inversion_template.format(class_decl if 'C' == t else '', default_value(t), t=t) for t in types if t != 'Bool']
for test in tests:
  write_counted(test)
